﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SchoolPOCOProxyDAL.POCOProxyEntities;

namespace SchoolPOCOProxyDAL
{
    class Class1
    {
            public static void UpdateOneToManyDemoInDisconnectedScenario()
        {
            Standard std = null;
            IList<Teacher> tchList = null;

            using (SchoolDBContext ctx = new SchoolDBContext())
            {
                //fetching existing standard from the db
                std = (from s in ctx.Standards
                                where s.StandardName == "standard 6"
                                select s).FirstOrDefault<Standard>();
                tchList = std.Teachers.ToList<Teacher>();
            }

                std.StandardName = "standard 6";
                std.Description = "Updated";

                //getting first teacher to be removed
                //Teacher tchr = std.Teachers.FirstOrDefault<Teacher>();
                //removing teachers (enable cascading delete for the teacher)
                //ctx.Teachers.DeleteObject(tchr);
            

            if(tchList != null)
            {
                if (tchList.Count >= 2)
                {
                    Teacher updateTchr = tchList[0];
                    updateTchr.TeacherName = "Upd Teachr";
                    tchList[0] = updateTchr; // Updated first teacher
                    tchList.RemoveAt(1);  // deleted second teacher
                }
            }

            // adding new teacher for selected standard
            Teacher newTeacher = new Teacher();
            newTeacher.TeacherName = "Teacher9";
            std.Teachers.Clear();
            std.Teachers.Add(newTeacher);
         
            //re-adding all updated, deleted and added teachers to standard
            foreach(Teacher techr in tchList)
                std.Teachers.Add(techr);


            using (SchoolDBContext newCtx = new SchoolDBContext())
            {
                //newCtx.Standards.in
                var exitingStandard = (from s in newCtx.Standards
                                       where s.StandardId == std.StandardId
                                       select s).FirstOrDefault<Standard>();

                var newTeachers = std.Teachers.ToList();
                var existingTeachers = exitingStandard.Teachers.ToList();

                var addedTeachers = newTeachers.Except<Teacher>(existingTeachers);
                var updatedTeachers = exitingStandard.Teachers.Intersect(newTeachers);
                var deletedTeachers = exitingStandard.Teachers.Except(std.Teachers);

                addedTeachers.ToList<Teacher>().ForEach(t => newCtx.Teachers.AddObject(t));

                foreach (Teacher t in updatedTeachers)
                {
                    newCtx.Teachers.Attach(t);
                    newCtx.ObjectStateManager.ChangeObjectState(t, System.Data.EntityState.Modified);
                }

                deletedTeachers.ToList<Teacher>().ForEach(t => newCtx.Teachers.DeleteObject(t));

                newCtx.SaveChanges();
                Console.WriteLine("POCOProxy UpdateOneToManyDemoInDisconnectedScenario Successfull");
            }
        }
    }
}
